package com.hiinfo.mvvmdemo.weather;

import android.app.Activity;
import android.util.Log;
import android.widget.Toast;

import com.hiinfo.mvvmdemo.base.BaseObjectBean;
import com.hiinfo.mvvmdemo.net.RetrofitClient;

import androidx.databinding.ObservableBoolean;
import androidx.databinding.ObservableField;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class WeatherViewModel {

    private static final String TAG = "WeatherViewModel";

    private Activity mContext;

    public final ObservableField<String> city = new ObservableField<>();

    public final ObservableField<String> cityId = new ObservableField<>();

    public final ObservableField<String> temp1 = new ObservableField<>();

    public final ObservableField<String> temp2 = new ObservableField<>();

    public final ObservableField<String> weather = new ObservableField<>();

    public final ObservableField<String> time = new ObservableField<>();

    public final ObservableField<String> userName = new ObservableField<>();
    public final ObservableField<String> password = new ObservableField<>();

    // 一个变量包含了所有关于View Style 相关的字段
    public final ViewStyle viewStyle = new ViewStyle();

    private Call<BaseObjectBean<WeatherInfo>> mCall;

    /**
     * ViewStyle 关于控件的一些属性和业务数据无关的Style 可以做一个包裹，这样代码比较美观，
     * ViewModel 页面也不会有太多太杂的字段。
     */
    public static class ViewStyle {

        public final ObservableBoolean loading = new ObservableBoolean(false);

        public final ObservableBoolean loadingSuccess = new ObservableBoolean(true);

        public final ObservableBoolean loadingFailure = new ObservableBoolean(false);
    }

    public WeatherViewModel(Activity context) {
        this.mContext = context;
    }

    public void queryWeather() {
        viewStyle.loading.set(true);
        viewStyle.loadingSuccess.set(false);
        viewStyle.loadingFailure.set(false);

        mCall = RetrofitClient.get()
            .create(WeatherRequest.class)
            .queryWeather();
        mCall.enqueue(new Callback<BaseObjectBean<WeatherInfo>>() {

            @Override
            public void onResponse(Call<BaseObjectBean<WeatherInfo>> call, Response<BaseObjectBean<WeatherInfo>> response) {

                BaseObjectBean<WeatherInfo> weatherData = response.body();
                if (weatherData == null) {
                    Log.i(TAG, "onResponse: call is null");
                    return;
                }

                WeatherInfo weatherInfo = weatherData.getWeatherinfo();
                city.set(weatherInfo.getCity());
                cityId.set(weatherInfo.getCityid());
                temp1.set(weatherInfo.getTemp1());
                temp2.set(weatherInfo.getTemp2());
                weather.set(weatherInfo.getWeather());
                time.set(weatherInfo.getPtime());

                viewStyle.loading.set(false);
                viewStyle.loadingSuccess.set(true);
            }

            @Override
            public void onFailure(Call<BaseObjectBean<WeatherInfo>> call, Throwable t) {
                if (call.isCanceled()) {
                    Log.i(TAG, "call is canceled.");
                } else {
                    viewStyle.loading.set(false);
                    viewStyle.loadingFailure.set(true);
                }
            }
        });
    }

    /**
     * 点击按钮登录
     */
    public void login() {
        Toast.makeText(mContext, "userName: " + userName.get() + ", password: " + password.get(), Toast.LENGTH_SHORT).show();
    }

    public void cancelRequest() {
        if (mCall != null) {
            mCall.cancel();
        }
    }
}
